<template>
  <div class="page">
    <sub-header :title="title"></sub-header>
    <div ref="lists">
      <div class='main' v-if="fav.length">
        <div class='goods-list' v-for="(item,index) in fav" :key="index"
             @click="$router.push({name:'goods-detail',query: {gid:item.gid}})">
          <div class='image'>
            <img v-lazy="item.image" alt=""/>
          </div>
          <div class='title'>{{item.title}}</div>
          <div class='price'>¥{{item.price}}</div>
          <div class='btn-wrap'>
            <div class='btn' @click.stop="$router.push({name:'goods-detail',query: {gid:item.gid}})">购买</div>
            <div class='btn' @click.stop="del(item.fid)">删除</div>
          </div>
        </div>
      </div>
      <div class="no-data" v-else>您还没有收藏商品！</div>
    </div>
  </div>
</template>

<script>
import SubHeader from 'components/SubHeader'
import { favListData, delFav } from '../../../api/fav'
import { mapState } from 'vuex'
import BScroll from 'better-scroll'

export default {
  name: 'fav',
  data () {
    return {
      fav: [],
      title: this.$route.meta.title
    }
  },
  computed: {
    ...mapState({
      uid: state => state.user.uid
    }),
  },
  methods: {
    initScroll () {
      let windowHeight = document.documentElement.clientHeight || document.body.clientHeight
      this.$refs.lists.style.height = windowHeight - this.$refs.lists.offsetTop + 'px'
      this.scroll = new BScroll(this.$refs.lists, {
        click: true,
        scrollY: true,
        pullUpLoad: true
      })
      this.scroll.on('pullingUp', async () => {
        this.page++
        let res = await favListData({
          uid: this.uid,
          page: this.page
        })
        if (res.code === 200) {
          this.fav.push(...res.data)
          this.$nextTick(() => {
            this.scroll.refresh()
            this.scroll.finishPullUp()
          })
        } else {
          this.$toast('没有更多收藏了')
        }
      })
    },
    async initFavDate () {
      this.page = 1
      let res = await favListData({
        uid: this.uid,
        page: this.page
      })
      if (res.code === 200) {
        this.fav = res.data
        this.$nextTick(() => {
          this.initScroll()
        })
      }
    },
    async del (fid) {
      let res = await delFav({
        uid: this.uid,
        fid: fid
      })
      if (res.code === 200) {
        for (let i = 0; i < this.fav.length; i++) {
          if (this.fav[i].fid === fid) {
            this.fav.splice(i, 1)
            this.$toast('删除成功')
            break
          }
        }
      } else {
        this.$toast(res.data)
      }
    }
  },
  created () {
    this.initFavDate()
    document.title = this.title
  },
  beforeDestroy () {
    this.scroll.destroyed()
    this.scroll = null
  },
  components: { SubHeader }
}
</script>

<style scoped>
  .page {
    width: 100%;
    min-height: 100vh;
    background-color: #FFFFFF;
  }

  .main {
    width: 100%;
    padding-top: 1.2rem;
    display: flex;
    display: -webkit-flex;
    justify-content: space-between;
    -webkit-justify-content: space-between;
    box-sizing: border-box;
    padding-left: 2%;
    padding-right: 2%;
    flex-wrap: wrap;
    -webkit-flex-wrap: wrap;
  }

  .main .goods-list {
    width: 48%;
    height: 5.8rem;
    margin-bottom: 0.3rem;
    overflow: hidden;
  }

  .main .goods-list .image {
    width: 3.36rem;
    height: 3.35rem;
    margin: 0 auto;
  }

  .main .goods-list .image img {
    width: 100%;
    height: 100%;
    border: 0 none;
  }

  .main .goods-list .title {
    width: 100%;
    height: 0.8rem;
    line-height: .4rem;
    overflow: hidden;
    font-size: 0.28rem;
    margin-top: 0.2rem;
  }

  .main .goods-list .price {
    font-size: 0.28rem;
    color: #F93036;
    margin-top: 0.2rem;
  }

  .main .goods-list .btn-wrap {
    width: 100%;
    display: flex;
    display: -webkit-flex;
    justify-content: space-between;
    -webkit-justify-content: space-between;
    margin-top: 0.2rem;
  }

  .main .goods-list .btn-wrap .btn {
    width: 1.04rem;
    height: 0.52rem;
    border: 1px solid #F93036;
    font-size: 0.28rem;
    color: #F93036;
    border-radius: 4px;
    text-align: center;
    line-height: 0.52rem;
  }
</style>
